package com.wing.cabinet.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wing.cabinet.model.response.AddAlarmVO;
import com.wing.cabinet.model.response.CellInventorySpaceVO;
import com.wing.common.utils.JsonResult;
import com.wing.order.model.entity.InventoryAlarm;
import com.wing.order.model.entity.InventoryAlarmDetail;
import com.wing.order.service.DynamicService;
import com.wing.order.service.InventoryAlarmDetailService;
import com.wing.order.service.InventoryAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AddAlarmDataServiceImpl implements AddAlarmDataService{

    @Autowired
    private  CellInventorySpaceService cellInventorySpaceService;
    @Autowired
    private InventoryAlarmService inventoryAlarmService;
    @Autowired
    private InventoryAlarmDetailService inventoryAlarmDetailService;
    @Autowired
    private DynamicService dynamicService;

    @Transactional
    @Override
    public JsonResult addAlarmData() {
        List<InventoryAlarm> listAlarm = inventoryAlarmService.list(new QueryWrapper<InventoryAlarm>().ne("status",3));//告警表
        List<AddAlarmVO> list = inventoryAlarmService.listAlarm();
        //已存在的不再添加 做判断 cabcode skucode  状态
        for (int k = 0;k<listAlarm.size();k++){
            for (int l = list.size() - 1; l >= 0; l--){
                if (listAlarm.get(k).getCabCode().equals(list.get(l).getCabCode())&&listAlarm.get(k).getSkuCode().equals(list.get(l).getSkuCode())&&listAlarm.get(k).getStatus().getValue()!=3){
                    if (list.get(l).getQuantity()>list.get(l).getAlarmStock()){
                        InventoryAlarm inventoryAlarm = new InventoryAlarm();
                        inventoryAlarm.setId(listAlarm.get(k).getId());
                        inventoryAlarm.setStatus(InventoryAlarm.Status.finish);
                        inventoryAlarmService.updateById(inventoryAlarm);
                    }
                    list.remove(l);
                }
            }
        }

        for (int j = 0;j<list.size();j++){
                InventoryAlarm inventoryAlarm = new InventoryAlarm();
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
                String code=dynamicService.getDayNextCode("ord_inventory_alarm");
                String day=simpleDateFormat.format(new Date());
                inventoryAlarm.setAlarmCode("GJ-"+day+"-"+code);
                inventoryAlarm.setDay(Integer.valueOf(day));
                inventoryAlarm.setNo(Integer.valueOf(code));
                inventoryAlarm.setAlarmStock(list.get(j).getAlarmStock());//告警库存
                inventoryAlarm.setBrandName(list.get(j).getBrandName());//产品品牌
                inventoryAlarm.setPlanReplenishment(list.get(j).getPlanReplenishment());//计划补货
                inventoryAlarm.setProductName(list.get(j).getProductName());//产品名称
                inventoryAlarm.setProposalStock(list.get(j).getProposalStock());//proposalStock
                inventoryAlarm.setSkuCode(list.get(j).getSkuCode());
                inventoryAlarm.setCabCode(list.get(j).getCabCode());
                inventoryAlarm.setStatus(InventoryAlarm.Status.pending);
                inventoryAlarmService.getBaseMapper().insert(inventoryAlarm);
                HashMap map = new HashMap();
                map.put("skuCode4app",list.get(j).getSkuCode());
                map.put("belongCabinet",list.get(j).getCabCode());
                List<CellInventorySpaceVO> listSpace = cellInventorySpaceService.list4app(map);
                for (int x= 0;x<listSpace.size();x++){
                    InventoryAlarmDetail inventoryAlarmDetail = new InventoryAlarmDetail();
                    inventoryAlarmDetail.setAlarmId(inventoryAlarm.getId());//主表id
                    inventoryAlarmDetail.setCabCode(listSpace.get(x).getCabCode());
                    inventoryAlarmDetail.setSpaceCode(listSpace.get(x).getSpaceCode());
                    inventoryAlarmDetail.setSkuCode(listSpace.get(x).getSkuCode());
                    inventoryAlarmDetail.setCurentStock(listSpace.get(x).getQuantity().longValue());
                    inventoryAlarmDetail.setAlarmCode("GJ-"+day+"-"+code);
                    inventoryAlarmDetail.setProductName(listSpace.get(x).getProductName());
                    inventoryAlarmDetail.setBrandName(listSpace.get(x).getProductBrandName());
                    inventoryAlarmDetail.setStatus(inventoryAlarm.getStatus().getDesc());
                    inventoryAlarmDetail.setSpaceCode(inventoryAlarmDetail.getSpecCode());
                    inventoryAlarmDetail.setProductType(listSpace.get(x).getProductTypeName());
                    inventoryAlarmDetail.setTheCategory(listSpace.get(x).getProductCategoryName());
                    inventoryAlarmDetailService.getBaseMapper().insert(inventoryAlarmDetail);//ord_inventory_alarm_detail
                }

        }
        return JsonResult.success("添加成功");
    }
}
